import { http } from "@/utils/http";

// 通用响应接口
export interface ApiResponse<T = any> {
  code: number;
  data?: T;
  message: string;
}

// 机构/岗位类型
export enum PositionType {
  ORGANIZATION = 0, // 机构
  DEPARTMENT = 1, // 部门
  POSITION = 2 // 岗位
}

// 机构/岗位层级
export enum PositionLevel {
  ORGANIZATION = 0, // 机构层级
  DEPARTMENT = 1, // 部门层级
  POSITION = 2 // 岗位层级
}

// 机构/岗位查询参数
export interface PositionQueryDto {
  posNumber?: string; // 机构编号
  type?: PositionType; // 类型
  level?: PositionLevel; // 层级
  cellId?: number; // 小区ID
}

// 部门/岗位列表查询参数
export interface PositionListDto {
  cellId: number; // 小区ID
  type: PositionType; // 类型
}

// 机构/岗位信息
export interface Position {
  id: number;
  cellId: number;
  cellName?: string;
  posNumber: string;
  name: string;
  ordered: number;
  parentId: number | null;
  type: PositionType;
  tenantId: number;
  isDeleted?: number;
  createTime: string;
  updateTime: string;
  parentName?: string | null;
  level?: PositionLevel;
  children?: Position[];
}

// 创建机构/岗位参数
export interface PositionCreateParams {
  posNumber: string;
  name: string;
  cellId?: number;
  parentId?: number;
  type: PositionType;
  ordered: number;
  tenantId?: number;
}

// 更新机构/岗位参数
export interface PositionUpdateParams {
  id: number;
  posNumber: string;
  name: string;
  cellId?: number;
  parentId?: number;
  type: PositionType;
  ordered: number;
  tenantId?: number;
}

// 获取机构/岗位树
export function getPositionTree(params?: PositionQueryDto) {
  return http.request<ApiResponse<Position[]>>("post", "/position/tree", {
    data: params || {}
  });
}

// 根据条件获取机构/岗位树
export function getPositionTreeByQuery(params: PositionQueryDto) {
  return http.request<ApiResponse<Position[]>>(
    "post",
    "/position/treeByQuery",
    {
      data: params
    }
  );
}

// 获取部门/岗位列表
export function getPositionList(params: PositionListDto) {
  return http.request<ApiResponse<Position[]>>(
    "post",
    "/position/listPosition",
    {
      data: params
    }
  );
}

// 根据ID获取机构/岗位详情
export function getPositionById(id: number) {
  return http.request<ApiResponse<Position>>(
    "get",
    "/position/getPositionById",
    {
      params: { id }
    }
  );
}

// 创建机构/岗位
export function createPosition(params: PositionCreateParams) {
  return http.request<ApiResponse<Position>>("post", "/position/add", {
    data: params
  });
}

// 更新机构/岗位
export function updatePosition(params: PositionUpdateParams) {
  return http.request<ApiResponse<Position>>("post", "/position/update", {
    data: params
  });
}

// 删除机构/岗位
export function deletePosition(id: number) {
  return http.request<ApiResponse<number>>("get", "/position/delete", {
    params: { id }
  });
}

// 批量删除机构/岗位
export function batchDeletePositions(ids: number[]) {
  return http.request<ApiResponse<null>>("post", "/position/batchDelete", {
    data: ids
  });
}
